package com.qikux.dao.impl;

import com.qikux.annotation.Component;
import com.qikux.dao.ResourceDao;
import com.qikux.entity.Resource;
import com.qikux.entity.ResourceComment;
import com.qikux.utils.DBUtils;
import com.qikux.vo.Page;

import java.util.List;
import java.util.Map;

@Component
public class ResourceDaoImpl implements ResourceDao {

    @Override
    public List<Map<String,Object>> findTypes() {
        String sql = "select t.id,t.type_name,t.pid,p.type_name as ptypename from tb_resource_type t left join tb_resource_type p on t.pid = p.id and t.pid is not null";
//        String sql = " select t.id , t.type_name ,t.pid from tb_resource_type t left join tb_resource_type p on t.pid = p.id and t.pid is null";
        return DBUtils.queryForList(sql);
    }


    @Override
    public void saveResource(Resource res) {
        String sql = "insert into tb_resource(resource_name, resource_desc, resource, keywords, type_id, score, size, ext,user_id,create_time) values(" +
                "#{resourceName}, #{resourceDesc}, #{resource}, #{keywords}, #{typeId}, #{score}, #{size}, #{ext}, #{userId}, now())" ;
        DBUtils.save(sql, res);
    }

    /**
     * 根据导航栏筛选资源
     * @param pid
     * @return
     */
    @Override
    public List<Map<String, Object>> findReses(String pid) {
        String sql = """
                select id,resource_name,resource_desc,keywords,type_id,score,resource,size,ext,create_time
                 from tb_resource where type_id = ?
                """;
        return DBUtils.queryForList(sql,pid);
    }
    /**
     * 热门资源
     */
    @Override
    public List<Map<String, Object>> findHot(int i) {
        String sql = """
                select id,hot,resource_name,resource_desc,keywords,type_id,score,resource,size,ext,create_time
                 from tb_resource where hot = ?
                """;
        return DBUtils.queryForList(sql,i);
    }

    @Override
    public List<Map<String, Object>> findHot(int i, Page<Map<String, Object>> pageBean) {
        String sql = """
                select id,hot,resource_name,resource_desc,keywords,type_id,score,resource,size,ext,create_time
                 from tb_resource where hot = ? and status=1 order by create_time desc limit ?,?
                """;
        return DBUtils.queryForList(sql,i,pageBean.getOffset(),pageBean.getRows());
    }

    @Override
    public int findResourceHot(int i) {
        String sql = """
                select count(1) from tb_resource where hot =? and status=1
                """;
        return DBUtils.execute(sql,rs -> {
            if (rs.next()){
                return rs.getInt(1);
            }
            return 0;
        },i);
    }

    /**
     * 导航栏查询
     * @param id
     * @param pageBean
     * @return
     */
    @Override
    public List<Map<String, Object>> findRes(int id, Page<Map<String, Object>> pageBean) {
        String sql = """
                select id,hot,resource_name,resource_desc,keywords,type_id,score,resource,size,ext,create_time
                 from tb_resource where type_id = ? and status=1 order by create_time desc limit ?,?
                """;
        return DBUtils.queryForList(sql,id,pageBean.getOffset(),pageBean.getRows());
    }

    @Override
    public int findResId(int id) {
        String sql = """
                select count(1) from tb_resource where type_id =? and status=1
                """;
        return DBUtils.execute(sql,rs -> {
            if (rs.next()){
                return rs.getInt(1);
            }
            return 0;
        },id);
    }

    /**
     * 全局模糊查询
     * @param fname
     * @param pageBean
     * @return
     */
    @Override
    public List<Map<String, Object>> findFuzzy(String fname, Page<Map<String, Object>> pageBean) {
        String sql = " select id,hot,resource_name,resource_desc,keywords,type_id,score,resource,size,ext,create_time from tb_resource where resource_name like concat('%',?,'%') order by create_time desc limit ?,? ";

        return DBUtils.queryForList(sql,fname,pageBean.getOffset(),pageBean.getRows());
    }

    @Override
    public int findResFuzzy(String fname) {
        String sql = "select count(1) from tb_resource where resource_name like concat('%',?,'%') and status=1";

        return DBUtils.execute(sql,rs -> {
            if (rs.next()){
                return rs.getInt(1);
            }
            return 0;
        },fname);
    }

    @Override
    public List<Map<String, Object>> qureyresource(Long userId) {
        String sql ="select resource_name,resource,score,ext,status,type_name from tb_resource left join tb_resource_type  t on type_id = t.id where user_id=?";
        return DBUtils.queryForList(sql,userId);

    }

    /**
     * @description 查询用户是否下载指定资源
     * @date 2022/9/28 11:52
     * @auther XuHuanDong
     * @return
     */
    @Override
    public List<Map<String, Object>> findDownloadResByUserIdAndResId(Long userId, Long resId) {
        String sql = "select * from tb_resource_history where user_id = ? and res_id = ?";
        return DBUtils.queryForList(sql, userId, resId);
    }

    /**
     * @description 查询用户是否评论过指定资源
     * @date 2022/9/28 11:53
     * @auther XuHuanDong
     */
    @Override
    public ResourceComment findResCommentByUserIdAndResId(Long userId, Long resId) {
        String sql = """
                select id ,user_id ,com_time ,com_content ,res_id ,stars ,com_status ,reason 
                from tb_resource_comment where user_id = ? and res_id = ? and com_status != -1
                """;
        return DBUtils.queryForEntity(sql,ResourceComment.class,userId,resId);
    }

    /**
     * @description 保存评论
     * @date 2022/9/28 11:54
     * @auther XuHuanDong
     */
    @Override
    public void saveResComment(ResourceComment resourceComment) {
        String sql = """
                insert into tb_resource_comment(user_id ,com_content ,res_id ,stars ,com_status,com_time)
                values(#{userId},#{comContent},#{resId},#{stars},#{comStatus},now())
                """;
        DBUtils.save(sql,resourceComment);
    }
}
